﻿using ModuleBase;
using ModuleBase.googoltech.GTS;
using Newtonsoft.Json;
using Prism.Ioc;

namespace GetPulseValue
{
    public class GetPulseValue : ModuleBase.ModuleBase
    {
        private IAxisCardService _axisCardService;
        bool outputs = false;
        bool isExecuted = false;
        private IServoModbusService _servoModbus;
        private IContainerProvider _containerProvider;

        private int _parameter1 = 1;
        public int Parameter1
        {
            get => _parameter1;
            set
            {
                if (_parameter1 != value)
                {
                    _parameter1 = value;
                    OnPropertyChanged(nameof(Parameter1));
                }
            }
        }
        private int _parameter2;
        public int Parameter2
        {
            get => _parameter2;
            set
            {
                if (_parameter2 != value)
                {
                    _parameter2 = value;
                    OnPropertyChanged(nameof(Parameter2));
                }
            }
        }
        private string _parameter3 = "Top_A8";
        public string Parameter3
        {
            get => _parameter3;
            set
            {
                if (_parameter3 != value)
                {
                    _parameter3 = value;
                    OnPropertyChanged(nameof(Parameter3));
                }
            }
        }
        private string _parameter4 = "COM1";
        public string Parameter4
        {
            get => _parameter4;
            set
            {
                if (_parameter4 != value)
                {
                    _parameter4 = value;
                    OnPropertyChanged(nameof(Parameter4));
                }
            }
        }
        public override Dictionary<string, object> GetProperties()
        {
            return new Dictionary<string, object>
            {
                { "轴号", Parameter1 },
                { "站号", Parameter2 },
                { "轴卡参数名", Parameter3 },
                { "端口", Parameter4 },
            };
        }

        public override Dictionary<string, List<object>> PropertyOptions { get; } = new Dictionary<string, List<object>>
        {
            { "轴号", new List<object> {  1,2,3,4,5,6,7,8 } },
            { "站号", new List<object> {  0,1,2,3,4,5,6,7,8,9 } },
            { "端口", new List<object> {  "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "COM10", "COM11", "COM12" } }
        };


        public GetPulseValue(IContainerProvider containerProvider)
        {
            _containerProvider = containerProvider;
            Name = "绝对值电机位置获取";

            DisplayNameToPropertyNameMap = new Dictionary<string, string>
            {
                { "轴号", nameof(Parameter1) },
                { "站号", nameof(Parameter2) },
                { "轴卡参数名", nameof(Parameter3) },
                { "端口", nameof(Parameter4) },
            };
        }

        public override bool Execute()
        {
            Status = ModuleStatus.Executing; // 设置状态为执行中

            //if (isExecuted)
            //{
            //    outputs = true;
            //    return outputs;
            //}
            _axisCardService = _containerProvider.Resolve<IAxisCardService>(Parameter3);
            _servoModbus = _containerProvider.Resolve<IServoModbusService>(Parameter4);
            try
            {
                AxisParm _axis = GetAxisByNumber(Parameter1);
                int pls = _servoModbus.ReadInovance((byte)Parameter2);
                GTSCard.AxisPosSet(_axis, pls * _axis.Equiv);
                GTSCard.AxisEncSet(_axis, pls * _axis.Equiv);
                GTSCard.AxisSynchAxisPos(_axis);
                GTSCard.SetSoftLimit(_axis);
                AxisStatus astatus = GTSCard.GetAxisStatus(_axis);
                outputs = true;
                isExecuted = true;
            }
            catch
            {
                outputs = false;
            }
            return outputs;
        }

        public override async Task<bool> ExecuteAsync()
        {

            outputs = false;
            var result = await Task.Run(() => Execute());
            return result;
        }

        public override void Initialize()
        {
            isExecuted = false;
        }
        private AxisParm GetAxisByNumber(int axisNumber)
        {
            switch (axisNumber)
            {
                case 1: return _axisCardService.A1;
                case 2: return _axisCardService.A2;
                case 3: return _axisCardService.A3;
                case 4: return _axisCardService.A4;
                case 5: return _axisCardService.A5;
                case 6: return _axisCardService.A6;
                case 7: return _axisCardService.A7;
                case 8: return _axisCardService.A8;
                default: return _axisCardService.A1;
            }
        }

        public override string Description => "绝对值电机位置获取模块：用于通过Modbus协议读取伺服电机的绝对位置并同步到轴卡。";
    }
}
